package com.xx.wars.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import android.graphics.Point;

import com.xx.framework.Music;
import com.xx.framework.Sound;
import com.xx.framework.gl.Texture;
import com.xx.framework.gl.TextureRegion;
import com.xx.framework.impl.GLGame;
import com.xx.wars.node.ResourceNode;

public class Assets {
	
	public static Music menu_Loop1;
//	public static Music[] game_Loops = new Music[5];
	public static Music[] game_Loops = new Music[1];
	
	
	public static Map<String, ResourceNode> fontCache;
	public static Texture font_texture;
	
	private static Map<String, ResourceNode> resCache;
	private static Map<String, Texture> resParentTextureCache;
	
	public static void loadAssets(GLGame game){
		try {
			resCache = new HashMap<String, ResourceNode>();
			resParentTextureCache = new HashMap<String, Texture>();

			InputStream in = game.getFileIO().readAsset("imgfilecfg.txt");
			BufferedReader re = new BufferedReader(new InputStreamReader(in, "utf-8"));
			String text = "";
			re.read(new char[1]);
			while ((text = re.readLine()) != null) {
				String[] nodeAtts = text.split("\t");
				ResourceNode node = new ResourceNode();
				node.setKey(nodeAtts[0].trim());
				node.setOriginalSize(new Point(
						Integer.parseInt(nodeAtts[1].trim()), Integer
								.parseInt(nodeAtts[2].trim())));
				node.setSize(new Point(Integer.parseInt(nodeAtts[3]), Integer
						.parseInt(nodeAtts[4])));
				node.setWhiteEdge(new Point(Integer.parseInt(nodeAtts[5]), Integer
						.parseInt(nodeAtts[6])));
				node.setLocation(new Point(Integer.parseInt(nodeAtts[7]), Integer
						.parseInt(nodeAtts[8])));
				node.setParentTexture(nodeAtts[9]);
				node.setParentTextureSize(new Point(Integer.parseInt(nodeAtts[10]),
						Integer.parseInt(nodeAtts[11])));
				resCache.put(node.getKey(), node);
			}
			Texture texture_0 = new Texture(game, "texture-0.png");
			resParentTextureCache.put(texture_0.getFileName(), texture_0);
			
			//font
			fontCache = new HashMap<String, ResourceNode>();
			in = game.getFileIO().readAsset("font/imgfilecfg.txt");
			re = new BufferedReader(new InputStreamReader(in, "utf-8"));
			text = "";
			re.read(new char[1]);
			while ((text = re.readLine()) != null) {
				String[] nodeAtts = text.split("\t");
				ResourceNode node = new ResourceNode();
				node.setKey(nodeAtts[0].trim());
				node.setOriginalSize(new Point(
						Integer.parseInt(nodeAtts[1].trim()), Integer
						.parseInt(nodeAtts[2].trim())));
				node.setSize(new Point(Integer.parseInt(nodeAtts[3]), Integer
						.parseInt(nodeAtts[4])));
				node.setWhiteEdge(new Point(Integer.parseInt(nodeAtts[5]), Integer
						.parseInt(nodeAtts[6])));
				node.setLocation(new Point(Integer.parseInt(nodeAtts[7]), Integer
						.parseInt(nodeAtts[8])));
				node.setParentTexture(nodeAtts[9]);
				node.setParentTextureSize(new Point(Integer.parseInt(nodeAtts[10]),
						Integer.parseInt(nodeAtts[11])));
				fontCache.put(node.getKey(), node);
			}
			
			font_texture = new Texture(game, "font/texture-0.png");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void loadSound(GLGame game) {
		menu_Loop1 = game.getAudio().newMusic("music/Menu_Loop1.m4a");
		for (int i = 0; i < game_Loops.length; i++) {
			game_Loops[i] = game.getAudio().newMusic("music/game_loop("+i+").m4a");
		}
	//	mainMenuMusic = game.getAudio().newMusic("music/MainMenu.m4a");
	}
	
	public static TextureRegion createTextureRegion(String string) {
		ResourceNode node = resCache.get(string);
		if(node == null){
			System.out.println("");
		}
		Texture texture = resParentTextureCache.get(node.getParentTexture());
		Point locatioPoint = node.getLocation();
		Point size = node.getSize();
		TextureRegion tr = new TextureRegion(texture, locatioPoint.x,
				locatioPoint.y, size.x, size.y);
		tr.setNode(node);
		return tr;
	}
	public static TextureRegion createFontTextureRegion(String string) {
		ResourceNode node = fontCache.get(string);
		if(node == null){
			System.out.println("ok");
		}
		Point locatioPoint = node.getLocation();
		Point size = node.getSize();
		TextureRegion tr = new TextureRegion(font_texture, locatioPoint.x,
				locatioPoint.y, size.x, size.y);
		tr.setNode(node);
		return tr;
	}
	
	public static void playSound(Sound sound) {
		if(sound == null)return;
		if (GameSetting.soundON)
			sound.play(1);
	}

	public static void playMusic(Music music) {
		if(music == null)return;
		if (GameSetting.musicON)
			music.play();
	}
}
